﻿using System;
using System.Threading;

namespace SimpleTerminal.Session
{
    public abstract class IConnection : IDisposable
    {

        private object tag;
        private string name;

        public delegate void SessionEventdHandler(IConnection session);
        public delegate void SessionEventdHandler<T>(IConnection session, T args);

        public SessionEventdHandler Connected;
        public SessionEventdHandler Disconnected;
        public SessionEventdHandler<DataEventArgs> DataReceived;
        public SessionEventdHandler<ErrorEventArgs> Error;
        public SessionEventdHandler<ConnectingEventArgs> Connecting;


        public class Errors
        {
            public const int CONNECT_ERROR = 1;
            public const int AUTH_ERROR = 2;
            public const int WSAEACCES = 3;
            public const int WSAECONNREFUSED = 4;
            public const int ERROR = 100;
        }

        private bool canwrite = true;


        public enum SessionType
        {
            Ssh,
            Serial
        }

        public abstract SessionType GetSessionType();

        public object Tag { get => tag; set => tag = value; }
        public string TerminalName { get; set; }
        public uint Height { get; set; }
        public uint Width { get; set; }
        public uint Columns { get; set; }
        public uint Rows { get; set; }
        public bool Canwrite { get => canwrite; set => canwrite = value; }
        public string Name { get => name; set => name = value; }

        /// <summary>
        /// 实现会话连接的接口
        /// </summary>
        public abstract void Connect();

        public virtual void BeginConnect()
        {
            Thread childThread = new Thread(Connect)
            {
                IsBackground = true
            };
            childThread.Start();
        }
        public virtual void BeginDisconnect()
        {
            Thread childThread = new Thread(Disconnect)
            {
                Name = "BeginDisconnect",
                IsBackground = true
            };
            childThread.Start();
        }

        /// <summary>
        /// 实现会话断开的接口
        /// </summary>
        public abstract void Disconnect();

        protected void OnConnected()
        {
            Connected?.Invoke(this);
        }

        protected void OnDisconnected()
        {
            Disconnected?.Invoke(this);
        }
        protected void OnDataReceived(byte[] data)
        {
            DataReceived?.Invoke(this, new DataEventArgs { Data = data });
        }
        protected void OnConnecting(string connectName)
        {
            Connecting?.Invoke(this, new ConnectingEventArgs { ConnectName = connectName });
        }
        protected void OnError(int error, string msg)
        {
            Error?.Invoke(this, new ErrorEventArgs { Error = error, Msg = msg });
        }

        public virtual void Write(string text)
        {
            this.Send(text);
        }
        public abstract void Send(string text);

        public virtual void SendWindowChangeRequest(int columns, int rows, int width, int height)
        {

        }

        public abstract bool IsConnected();

        public virtual void SendKeepAlive()
        {

        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~IConnection()
        {
            Dispose(false);
        }

        protected bool _isDisposed = false;
        protected virtual void Dispose(bool disposing)
        {
            if (_isDisposed)
                return;

            if (disposing)
            {

            }
            _isDisposed = true;
        }
    }
}
